package com.idp.web.service.impl;

import com.idp.common.base.BaseResult;
import com.idp.common.base.BusinessException;
import com.idp.common.constants.CommonConstants;
import com.idp.common.constants.Constants;
import com.idp.common.persistence.Page;
import com.idp.web.dao.PatientDao;
import com.idp.web.dto.PatientListDTO;
import com.idp.web.entity.Patient;
import com.idp.web.service.IPatientService;
import com.idp.web.system.entity.SysUser;
import com.idp.web.system.service.SysUserService;
import com.idp.web.vo.PatientSearchVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 患者service实现类
 *
 * @author xubinren
 * @version 1.0
 * @date 2018/3/6
 */
@Service("patientService")
public class PatientServiceImpl implements IPatientService {

    @Resource
    private PatientDao patientDao;

    @Resource
    private SysUserService sysUserService;

    /**
     * 根据条件分页查询患者列表
     *
     * @param patientSearchVo 查询条件
     * @param page 分页信息
     * @return Page<PatientListDTO>
     */
    @Override
    public Page<PatientListDTO> findByPage(PatientSearchVo patientSearchVo, Page<PatientListDTO> page) {
        if(null != patientSearchVo && StringUtils.isNotEmpty(patientSearchVo.getNameOrPhone())){
            patientSearchVo.setNameOrPhone(patientSearchVo.getNameOrPhone());
        }
        page.setResult(patientDao.selectPage(page,patientSearchVo));
        return page;
    }

    @Override
    public List<Patient> findBySearch(Patient patient) {
        return patientDao.find(patient);
    }

    /**
     * 通过ID查询
     *
     * @param id 患者id
     * @return Patient
     */
    @Override
    public Patient getById(String id) {
        return patientDao.getById(id);
    }

    /**
     * 新建患者信息
     * @param patient 患者信息
     * @param userId 操作人id
     * @return BaseResult
     */
    @Transactional(rollbackFor = BusinessException.class)
    @Override
    public BaseResult add(Patient patient, String userId) {

        patient.preInsert(userId);
        patient.setAccountStastus(CommonConstants.PATIENT_STATUS_DISABLED);

        Integer changeNum = patientDao.add(patient);
        if (null == changeNum || changeNum < CommonConstants.INT_ONE){
            return new BaseResult(Constants.RESPONSE_CODE_605,Constants.RESPONSE_MESSAGE_605);
        }

        //同步到用户信息
        SysUser user = new SysUser();
        BeanUtils.copyProperties(patient,user);
        user.setUsername(patient.getUserName());
        user.setUserType(CommonConstants.USER_TYPE_PATIENT);
        sysUserService.registerAdd(user);

        return new BaseResult(Constants.RESPONSE_CODE_200,Constants.RESPONSE_MESSAGE_200);
    }

    /**
     * 修改患者信息
     * @param patient 修改后的患者信息
     * @param userId 操作人id
     * @return BaseResult
     */
    @Transactional(rollbackFor = BusinessException.class)
    @Override
    public BaseResult update(Patient patient, String userId) {
        Patient oldPatient = getById(patient.getId());
        patient.preUpdate(userId);

        patient.setCreateUser(oldPatient.getCreateUser());
        patient.setCreateDate(oldPatient.getCreateDate());
        patient.setLogicDelete(oldPatient.getLogicDelete());
        patient.setAccountStastus(oldPatient.getAccountStastus());

        Integer changeNum = patientDao.update(patient);
        if (null == changeNum || changeNum < CommonConstants.INT_ZERO) {
            return new BaseResult(Constants.RESPONSE_CODE_605, Constants.RESPONSE_MESSAGE_605);
        }

        //同步到用户信息
        SysUser user = sysUserService.getById(patient.getId());
        BeanUtils.copyProperties(patient, user);
        user.setUserType(CommonConstants.USER_TYPE_DOCTOR);
        user.setUsername(patient.getUserName());
        user.preUpdate(patient.getCreateUser());
        user.setId(patient.getId());
        sysUserService.update(user);

        return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200);
    }

    /**
     * 根据患者id删除对应的患者信息
     * @param id 患者id
     * @return boolean
     */
    @Transactional(rollbackFor = BusinessException.class)
    @Override
    public boolean delete(String id) {
        Integer deletePatientNum = patientDao.delete(id);
        if (null == deletePatientNum || deletePatientNum < CommonConstants.INT_ONE){
            return false;
        }

        sysUserService.delete(id);
        return true;
    }

    /**
     * 修改用户账户状态
     * @param id 用户id
     * @param userId 操作人id
     * @return BaseResult
     */
    @Transactional(rollbackFor = BusinessException.class)
    @Override
    public BaseResult changeStatus(String id, String userId) {
        if (StringUtils.isEmpty(id)) {
            return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400);
        }
        //根据用户id获取对应的用户信息
        Patient patient = getById(id.trim());
        //账户状态：1、可使用；0、禁用
        Integer accountStauts = patient.getAccountStastus();

        //排除错误数据
        if (null == accountStauts || accountStauts < CommonConstants.INT_ZERO || accountStauts > CommonConstants.INT_ONE) {
            return new BaseResult(Constants.RESPONSE_CODE_604, Constants.RESPONSE_MESSAGE_604);
        }

        //修改状态
        if (CommonConstants.PATIENT_STATUS_ENABLED == accountStauts) {
            //改为禁用
            patient.setAccountStastus(CommonConstants.PATIENT_STATUS_DISABLED);
        } else {
            //改为启用
            patient.setAccountStastus(CommonConstants.PATIENT_STATUS_ENABLED);
        }

        patient.preUpdate(userId);
        patientDao.update(patient);

        return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200);
    }
}